package com.kerpow.KSheet.character;

import java.io.File;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.kerpow.KSheet.CharacterNote;
import com.kerpow.KSheet.KSheet;
import com.kerpow.KSheet.character.Stat.StatType;


public class CharacterAdapter {	
	public enum LoadState{
		Ready,
		MustReload,
		CannotReload,
		Unkown
	}
	
	private int ERROR = 99;
	
    private DatabaseHelper _dbHelper;
    private SQLiteDatabase _db;
	
    private final Context _context;
    
    private final String MARK_RELOAD = "RELOAD";
    
    private static final String DATABASE_NAME = "SheetViewer";
    private static final int DATABASE_VERSION = 8;
    
    public static final String TABLE_CHARACTERS = "characters";
    public static final String FIELD_CHARACTERS_ID = "_id";
    public static final String FIELD_CHARACTERS_PATH = "path";
    public static final String FIELD_CHARACTERS_MD5 = "md5";

    public static final String TABLE_POWERS = "powers";
    public static final String FIELD_POWERS_ID = "_id";
    public static final String FIELD_POWERS_CHARACTERID = "characterid";
    public static final String FIELD_POWERS_NAME = "name";
    public static final String FIELD_POWERS_ATTACKBONUS = "attack";
    public static final String FIELD_POWERS_DAMAGE = "damage";
    public static final String FIELD_POWERS_DEFENSE = "defense";
    public static final String FIELD_POWERS_USAGE = "usage";
    public static final String FIELD_POWERS_USAGE_SORT = "usagesort";
    public static final String FIELD_POWERS_ACTION = "action";
    public static final String FIELD_POWERS_ACTION_SORT = "actionsort";
    public static final String FIELD_POWERS_WEBDETAIL = "webdetail";
    public static final String FIELD_POWERS_WEBURL = "weburl";
    public static final String FIELD_POWERS_USED = "used";
    public static final String FIELD_POWERS_SHOW = "show";
    
    public static final String TABLE_STATS = "stats";
    public static final String FIELD_STATS_ID = "_id";
    public static final String FIELD_STATS_CHARACTERID = "characterid";
    public static final String FIELD_STATS_NAME = "name";
    public static final String FIELD_STATS_VALUE = "value";
    public static final String FIELD_STATS_TYPE = "stattype"; 
    public static final String FIELD_STATS_WEBDETAIL = "webdetail";
	public static final String FIELD_STATS_WEBURL = "weburl";
    
    public static final String TABLE_NOTES = "notes";
    public static final String FIELD_NOTES_ID = "_id";
    public static final String FIELD_NOTES_CHARACTERID = "characterid";
    public static final String FIELD_NOTES_NAME = "name";
    public static final String FIELD_NOTES_NOTE = "note";
    public static final String FIELD_NOTES_MODIFIED = "modified";
       
    private static final String SQL_CREATE_CHARACTERS =
    	"create table characters (_id integer primary key autoincrement, path text not null, md5 text not null);";
	
    private static final String SQL_CREATE_POWERS =
    	"create table powers (_id integer primary key autoincrement, characterid integer not null, name text not null, " +
    							"attack text, damage text, defense text, usage text not null, action text not null, "+
    							"webdetail text not null, weburl text not null, used boolean not null, show boolean not null, "+
    							"usagesort int not null, actionsort int not null);";
    
    private static final String SQL_CREATE_STATS =
    	"create table stats (_id integer primary key autoincrement, characterid integer not null, name text not null, " +
    							"value text not null, stattype int not null, webdetail text not null, weburl text not null);";
    
    private static final String SQL_CREATE_NOTES =
    	"create table notes (_id integer primary key autoincrement, characterid integer not null, name text not null, " +
			"note text not null, modified text not null);";
 	
	public enum PowerSortMethod{
		Usage,
		Action,
		Name
	}
   private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(SQL_CREATE_CHARACTERS);
            db.execSQL(SQL_CREATE_POWERS);
            db.execSQL(SQL_CREATE_STATS);
            db.execSQL(SQL_CREATE_NOTES);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        	if(oldVersion == 7 && newVersion == 8){
        		db.execSQL(SQL_CREATE_NOTES);
        	}else{

                db.execSQL("DROP TABLE IF EXISTS characters");
                db.execSQL("DROP TABLE IF EXISTS powers");
                db.execSQL("DROP TABLE IF EXISTS stats");
                           
                onCreate(db);	
        	}
        }
    }

   	public CharacterAdapter(Context c){
   		_context = c;
   	}
   	
   	public Stat getStat(Long characterID, String name){
   		name = name.replace("'", "");
   		Cursor c = _db.query(TABLE_STATS, new String[]{FIELD_STATS_NAME,FIELD_STATS_VALUE,FIELD_STATS_TYPE}, FIELD_STATS_CHARACTERID + "="+characterID+" and "+FIELD_STATS_NAME+"='"+name+"'", null, null, null, null);
   		if(c.moveToFirst()){
   			Stat s = new Stat();
   			s.Name = c.getString(0);
   			s.Value = c.getString(1);
   			
   			int stattype = c.getInt(2);
   			for(StatType t: StatType.values()){
   				if(t.ordinal() == stattype){
   					s.Type = t;
   					break;
   				}
   			}
   			c.close();
   			return s;
   		}
   		c.close();
   		return new Stat(name,"-",StatType.other);
   	}
   	public Stat getStat(Long characterID, String name, Stat.StatType type){
   		name = name.replace("'", "");
   		Cursor c = _db.query(TABLE_STATS, new String[]{FIELD_STATS_NAME,FIELD_STATS_VALUE,FIELD_STATS_TYPE}, 
   					FIELD_STATS_CHARACTERID + "="+characterID+" and "+FIELD_STATS_NAME+"='"+name+"' and " + FIELD_STATS_TYPE+"="+type.ordinal(), null, null, null, null);
   		
   		if(c.moveToFirst()){
   			Stat s = new Stat();
   			s.Name = c.getString(0);
   			s.Value = c.getString(1);
   			
   			int stattype = c.getInt(2);
   			for(StatType t: StatType.values()){
   				if(t.ordinal() == stattype){
   					s.Type = t;
   					break;
   				}
   			}
   			c.close();
   			return s;
   		}
   		c.close();
   		return new Stat(name,"-",StatType.other);
   	}
   	public void setStat(long characterID, String name, String value){
   		ContentValues values = new ContentValues();
  		name = name.replace("'", "");
  		value = value.replace("'", "");
  		values.put(FIELD_STATS_VALUE, value);
   		
   		_db.update(TABLE_STATS, values, FIELD_STATS_NAME + "='" + name +"' and " + FIELD_STATS_CHARACTERID + "=" + characterID , null);
   	}
   	
    public CharacterAdapter open() throws SQLException {
    	if(_db == null || !_db.isOpen()){
    	
        _dbHelper = new DatabaseHelper(_context);
        _db = _dbHelper.getWritableDatabase();
    	}
        return this;
    }

    
    public void close() {
    	_db.close();
    	_dbHelper.close();
    }
	/*
    //creates the character if it doesn't exist, otherwise uses the old id
    public long createOrUpdateCharacter(String path, String md5){
    	Long id = this.getCharacterId(path);
    	if(id != null){
    		
        	ContentValues v = new ContentValues();
        	v.put(FIELD_CHARACTERS_MD5, md5);
        	_db.update(TABLE_CHARACTERS, v, FIELD_CHARACTERS_PATH + "='" + path+"'", null);
    		
    		return id;
    	}
    		
    	ContentValues v = new ContentValues();
    	v.put(FIELD_CHARACTERS_PATH, path);
    	v.put(FIELD_CHARACTERS_MD5, md5);
    	_db.insert(TABLE_CHARACTERS, null,v);
    	
    	return getCharacterId(path);
    }
    */
    public String getCharacterMd5(Long id){
    	String ret = null;
    	Cursor c = _db.query(TABLE_CHARACTERS, new String[] {FIELD_CHARACTERS_ID, FIELD_CHARACTERS_MD5}, FIELD_CHARACTERS_ID + "='"+id+"'", null, null, null, null);
    	if(c!=null){
    		if(c.moveToFirst()){
        		ret = c.getString(c.getColumnIndex(FIELD_CHARACTERS_MD5));
    		}
    		c.close();
    		return ret;
    	}
    	return null;
    }
    
    public LoadState getLoadState(String path){
    	String md5="";
    	long pathId = -1;
    	long md5Id = -1;
    	boolean exist = new File(path).exists();
    	
    	pathId = getIDbyPath(path);
    	if(exist) {
    		md5 = KSheet.md5(path);
    		md5Id = getIDbyMD5(md5);
    	}
    	
    	if(md5Id == -1 && pathId == -1)
    		return LoadState.Unkown;
    	

    	
    	if(pathId == md5Id) //file is exactly how it should be
    		return LoadState.Ready;
    	
    	if(md5Id != -1){
    		//file moved, update path and return ready
        	ContentValues v = new ContentValues();
        	v.put(FIELD_CHARACTERS_PATH, path);
        	_db.update(TABLE_CHARACTERS, v, FIELD_CHARACTERS_ID + "=" + md5Id, null);
        	return LoadState.Ready;
    	}    	

    	if(pathId != -1) { 
    		if(!exist){
	    		//reload is not an option
	    		return LoadState.CannotReload;
    		}
    		//file changed, mark for reload and return mustreload
        	ContentValues v = new ContentValues();
        	v.put(FIELD_CHARACTERS_MD5, MARK_RELOAD);
        	_db.update(TABLE_CHARACTERS, v, FIELD_CHARACTERS_ID + "=" + pathId, null);
        	return LoadState.MustReload;
    	}
    	
    	//if(md5Id == -1 && pathId == -1){
    		//file completely unkown, no reason to actually check this
    		

    	return LoadState.Unkown;

    }
    
    public long createCharacterOrPrepForParse(String path){
    	LoadState s = getLoadState(path);
    	
    	switch(s){
    	case Unkown:
    		ContentValues v = new ContentValues();
        	v.put(FIELD_CHARACTERS_PATH, path);
        	v.put(FIELD_CHARACTERS_MD5, MARK_RELOAD);
        	return _db.insert(TABLE_CHARACTERS, null,v);
    	case MustReload:
    	case Ready:
    		long id = getCharacterID(path);
        	_db.delete(TABLE_POWERS, FIELD_POWERS_CHARACTERID + "=" + id, null);
        	_db.delete(TABLE_STATS, FIELD_STATS_CHARACTERID + "=" + id, null);
    		return id;
    	}
    	return -1;
    	
    }
    
    //find the character id associated with this file
    public long getCharacterID(String path){
    	return getIDbyPath(path);
    }

    private long getIDbyPath(String path){
    	long ret = -1;


    	Cursor c = _db.query(TABLE_CHARACTERS, new String[] {FIELD_CHARACTERS_ID, FIELD_CHARACTERS_PATH}, FIELD_CHARACTERS_PATH + "=?", 
				new String[] {path}, null, null, null);
    	if(c!=null){
    		if(c.moveToFirst()){
        		ret = c.getLong(c.getColumnIndex(FIELD_CHARACTERS_ID));
    		}

    		c.close();
    	}
    	return ret;
    }
    private long getIDbyMD5(String md5){
    	long ret = -1;
    	Cursor c = _db.query(TABLE_CHARACTERS, new String[] {FIELD_CHARACTERS_ID, FIELD_CHARACTERS_PATH}, FIELD_CHARACTERS_MD5 + "='"+md5+"'", null, null, null, null);
    	if(c!=null){
    		if(c.moveToFirst()){
        		ret = c.getLong(c.getColumnIndex(FIELD_CHARACTERS_ID));
    		}

    		c.close();
    	}
    	return ret;
    }

    public void addPower(Long characterID, Power p){
    	ContentValues values = new ContentValues();

    	int actionsort=0, usagesort=0;

    	
    	usagesort = getUsageSort(p.Usage);
    	actionsort = getActionSort(p.ActionType);
    	
    	//override basics to always put them at the bottom
    	if(p.Name.equals("Melee Basic Attack")){
    		p.WebDetail = "<h1 class=\"dailypower\"><span class=\"level\"></span>Melee Basic Attack</h1><p class=\"flavor\"><i>You basically attack, basically.</i></p><p class=\"powerstat\"><b>At-Will</b>&nbsp;&nbsp; <img src=\"images/bullet.gif\" alt=\"\">&nbsp;&nbsp;&nbsp;&nbsp; <b>Martial</b>, <b>Weapon</b><br><b>Standard Action</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Melee</b> weapon</p><p class=\"powerstat\"><b>Target</b>: One creature</p><p class=\"powerstat\"><b>Attack</b>: Strength vs. AC</p><p class=\"flavor\"><b>Hit</b>: 1[W] + Strength modifier damage.</p>";
    		actionsort = 10;
    		usagesort = 10;
    	}else if(p.Name.equals("Ranged Basic Attack")) {
    		p.WebDetail = "<h1 class=\"dailypower\"><span class=\"level\"></span>Ranged Basic Attack</h1><p class=\"flavor\"><i>You basically attack, basically.</i></p><p class=\"powerstat\"><b>At-Will</b>&nbsp;&nbsp; <img src=\"images/bullet.gif\" alt=\"\">&nbsp;&nbsp;&nbsp;&nbsp; <b>Martial</b>, <b>Ranged Weapon</b><br><b>Standard Action</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Ranged</b> weapon</p><p class=\"powerstat\"><b>Target</b>: One creature</p><p class=\"powerstat\"><b>Attack</b>: Dexterity vs. AC</p><p class=\"flavor\"><b>Hit</b>: 1[W] + Dexterity modifier damage.</p>";
    		actionsort = 10;
    		usagesort = 10;
    	}else 
    		p.WebDetail="";
    	
    	values.put(FIELD_POWERS_CHARACTERID, characterID);
    	values.put(FIELD_POWERS_NAME, p.Name);
    	values.put(FIELD_POWERS_ATTACKBONUS, p.AttackBonus);
    	values.put(FIELD_POWERS_DEFENSE, p.Defense);
    	values.put(FIELD_POWERS_DAMAGE, p.Damage);
    	values.put(FIELD_POWERS_USAGE, p.Usage);
    	values.put(FIELD_POWERS_ACTION, p.ActionType);
    	values.put(FIELD_POWERS_WEBURL, "");
    	values.put(FIELD_POWERS_WEBDETAIL, p.WebDetail);
    	values.put(FIELD_POWERS_USED, false);
    	values.put(FIELD_POWERS_SHOW, true);
    	values.put(FIELD_POWERS_USAGE_SORT, usagesort);
    	values.put(FIELD_POWERS_ACTION_SORT, actionsort);
    	
    	//Log.i("kerpow", p.Name + " " + p.ActionType + " " + actionsort + " " + p.Usage + " " + usagesort );
    	
    	_db.insert(TABLE_POWERS, null, values);
    }
    
    public void setCharacterLoaded(long characterID, String path){
    	ContentValues v = new ContentValues();
    	
    	v.put(FIELD_CHARACTERS_MD5, KSheet.md5(path));
    	
    	_db.update(TABLE_CHARACTERS, v, FIELD_CHARACTERS_ID+"="+characterID, null);
    }
    
    public void setPowerHidden(Long long1){
    	ContentValues values = new ContentValues();
    	
    	values.put(FIELD_POWERS_SHOW, 0);
    	
    	_db.update(TABLE_POWERS, values, FIELD_POWERS_ID+"="+long1, null);
    }
    
    public void setPowersShown(Long characterID){
    	ContentValues values = new ContentValues();
    	
    	values.put(FIELD_POWERS_SHOW, 1);
    	
    	_db.update(TABLE_POWERS, values, FIELD_POWERS_CHARACTERID+"="+characterID, null);
    }
    public void setPowerUsed(Long long1, boolean isUsed){
    	
    	ContentValues values = new ContentValues();
    	
    	values.put(FIELD_POWERS_USED, isUsed ? 1 : 0);
    	
    	_db.update(TABLE_POWERS, values, FIELD_POWERS_ID+"="+long1, null);
    }
    
    public void setPowersUnUsed(Long characterID){
    	ContentValues values = new ContentValues();
    	
    	values.put(FIELD_POWERS_USED, 0);
    	
    	_db.update(TABLE_POWERS, values, FIELD_POWERS_CHARACTERID+"="+characterID, null);
    }
    
    public void setPowerWebDetail(Long powerId, String detail){
    	ContentValues values = new ContentValues();
    	
    	//correct the action and usage sorts
		String regex = "(\\w*) Action</b>";
		Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
		Matcher matcher = pattern.matcher(detail);
		if(matcher.find())
			values.put(FIELD_POWERS_ACTION_SORT, getActionSort(matcher.group(1)));
    	
		regex = "<p class=\"powerstat\"><b>(.*?)</b>";
		pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
		matcher = pattern.matcher(detail);
		if(matcher.find())
			values.put(FIELD_POWERS_USAGE_SORT, getUsageSort(matcher.group(1)));		
		
    	values.put(FIELD_POWERS_WEBDETAIL, detail);
    	
    	_db.update(TABLE_POWERS, values, FIELD_POWERS_ID+"="+powerId, null);
    }
    
    private int getUsageSort(String action){
    	//Log.i("kerpow", action);
    	if(action != null){
	    	if(action.contains("A")) return 1;
	    	if(action.contains("E")) return 2;
	    	if(action.contains("D")) return 3;
    	}
    	return ERROR;
    }
    private int getActionSort(String action){
    	//Log.i("kerpow", action);
    	if(action != null){
    		if(action.contains("No")) return 0;
	    	if(action.contains("Standard")) return 1;
	    	if(action.contains("Move")) return 2;
	    	if(action.contains("Minor")) return 3;
	    	if(action.contains("Free")) return 4;
	    	if(action.contains("Immediate")) return 5;
    	}
    	return ERROR;
    }
    
    public Cursor fetchPower(Long id){
    	
    	Cursor c = _db.query(TABLE_POWERS, null, FIELD_POWERS_ID+"="+id, null, null, null, null);
    	c.moveToFirst();
    	return c;
    }
    
    public Cursor fetchPowers(Long characterID,PowerSortMethod sortMethod){
    	Cursor c = null;
    	switch(sortMethod){
    	case Action:
    		c = _db.query(TABLE_POWERS, null, FIELD_POWERS_CHARACTERID+"="+characterID+" and "+FIELD_POWERS_SHOW +"=1", null, null, null, 
    				FIELD_POWERS_USED + " ASC, " + FIELD_POWERS_ACTION_SORT +" ASC, " + FIELD_POWERS_USAGE_SORT +" ASC");
    		break;
    	case Name:
    		c = _db.query(TABLE_POWERS, null, FIELD_POWERS_CHARACTERID+"="+characterID+" and "+FIELD_POWERS_SHOW +"=1", null, null, null, FIELD_POWERS_NAME +" ASC");
    		break;
    	case Usage:
    		c = _db.query(TABLE_POWERS, null, FIELD_POWERS_CHARACTERID+"="+characterID+" and "+FIELD_POWERS_SHOW +"=1", null, null, null, FIELD_POWERS_USAGE_SORT +" ASC");
    		break;
    	}
    	
    	return c;
    }
    public Cursor fetchStats(Long characterID, StatType type){
    	Cursor c = _db.query(TABLE_STATS, null, FIELD_STATS_CHARACTERID+"="+characterID+" and "+FIELD_STATS_TYPE+"="+type.ordinal(), null, null, null, FIELD_STATS_NAME +" ASC");
    	
    	return c;
    }  
    
    public void addStat(Long characterID, Stat s){
    	if(s.Value == null) s.Value = "";
    	s.Name = s.Name.replace("'", "");
    	s.Value = s.Value.replace("'", "");
    	
    	ContentValues values = new ContentValues();
    	
    	values.put(FIELD_STATS_CHARACTERID, characterID);
    	values.put(FIELD_STATS_NAME, s.Name);
    	values.put(FIELD_STATS_VALUE, s.Value);
    	values.put(FIELD_STATS_TYPE, s.Type.ordinal());
    	values.put(FIELD_STATS_WEBDETAIL, "");
    	values.put(FIELD_STATS_WEBURL, "");   	

    	
    	_db.insert(TABLE_STATS, null, values);
    }  
    
    public void setEncounterPowersUnused(Long characterID){
    	ContentValues values = new ContentValues();
    	
    	values.put(FIELD_POWERS_USED, 0);
    	
    	_db.update(TABLE_POWERS, values, FIELD_POWERS_CHARACTERID+"="+characterID + " and " + FIELD_POWERS_USAGE + "='Encounter'", null);
    }

    
	@SuppressWarnings("unchecked")
	public static ArrayList<Power> sortPowers(ArrayList<Power> powers, PowerSortMethod method){
		
		ArrayList<Power> ret = (ArrayList<Power>) powers.clone();
		switch(method){
		case Usage:
			Collections.sort(ret, new Comparator<Power>() {

				@Override
				public int compare(Power object1, Power object2) {
					Integer left=-1, right=-1;
					if(object1.Usage.contains("At"))left=0;
					if(object1.Usage.contains("En"))left=1;
					if(object1.Usage.contains("Da"))left=2;
					
					if(object2.Usage.contains("At"))right=0;
					if(object2.Usage.contains("En"))right=1;
					if(object2.Usage.contains("Da"))right=2;
					
					return left.compareTo(right);
				}
			});
			break;
		case Name:
			Collections.sort(ret, new Comparator<Power>() {

				@Override
				public int compare(Power object1, Power object2) {
					return object1.Name.compareTo(object2.Name);
				}
			});
			break;
		case Action:
			Collections.sort(ret, new Comparator<Power>() {

				@Override
				public int compare(Power object1, Power object2) {
					
					int left=0, right=0;
					if(object1.ActionType.contains("S"))left=10;
					if(object1.ActionType.contains("Mo"))left=20;
					if(object1.ActionType.contains("Mi"))left=30;
					if(object1.ActionType.contains("F"))left=40;
					if(object1.ActionType.contains("I"))left=50;
					
					if(object2.ActionType.contains("S"))right=10;
					if(object2.ActionType.contains("Mo"))right=20;
					if(object2.ActionType.contains("Mi"))right=30;
					if(object2.ActionType.contains("F"))right=40;
					
					if(left < right) return -1;
					if(right < left) return 1;
					return 0;
					//return object1.ActionType.compareTo(object2.ActionType);
				}
			});
			break;
		}
		
		return ret;
	}
	
	public ArrayList<File> fetchDBPaths(){
    	Cursor c = _db.query(TABLE_CHARACTERS, new String[]{FIELD_CHARACTERS_PATH}, null, null, null, null, FIELD_CHARACTERS_ID + " ASC");
    	ArrayList<File> ret = new ArrayList<File>();
    	if(c.moveToFirst()){
    		do{
    			ret.add(new File(c.getString(0)));
    		}
    		while(c.moveToNext());
    			
    	}
    	c.close();
    	
    	return ret;
	}
	
	public void createNote(long characterID, String note){
    	ContentValues values = new ContentValues();
    	
    	values.put(FIELD_NOTES_CHARACTERID, characterID);
    	values.put(FIELD_NOTES_NOTE, note);
    	values.put(FIELD_NOTES_NAME, "");
    	values.put(FIELD_NOTES_MODIFIED, DateFormat.getDateTimeInstance().format(new Date()));
    	
    	_db.insert(TABLE_NOTES, null, values);
	}
	
	public ArrayList<CharacterNote> reviewNotes(long characterID){
		ArrayList<CharacterNote> ret = new ArrayList<CharacterNote>();
		
		Cursor c = _db.query(TABLE_NOTES, null, FIELD_NOTES_CHARACTERID+"="+characterID, null, null, null, FIELD_NOTES_MODIFIED + " DESC");
		if(c.moveToFirst())
			do{
				ret.add(CharacterNote.from(c));
				
			}while(c.moveToNext());
		
		c.close();
		return ret;
	}
	
	
	public void updateNote(long noteID, String note){
    	ContentValues values = new ContentValues();
    	
    	values.put(FIELD_NOTES_NOTE, note);
    	values.put(FIELD_NOTES_MODIFIED, DateFormat.getDateTimeInstance().format(new Date()));

    	_db.update(TABLE_NOTES, values, FIELD_NOTES_ID + "=" + noteID, null);
	}
	
	public void deleteCharacter(long id){
		_db.delete(TABLE_CHARACTERS, FIELD_CHARACTERS_ID+"="+id, null);
		_db.delete(TABLE_POWERS, FIELD_POWERS_CHARACTERID+"="+id, null);
		_db.delete(TABLE_STATS, FIELD_STATS_CHARACTERID+"="+id, null);
		_db.delete(TABLE_NOTES, FIELD_NOTES_CHARACTERID+"="+id, null);
		
	}
	public void deleteNote(long noteID){
		_db.delete(TABLE_NOTES, FIELD_NOTES_ID+"="+noteID, null);
	}
    public void setPowerWebURL(long powerId, String url){
    	ContentValues values = new ContentValues();
    	values.put(FIELD_POWERS_WEBURL, url);

    	_db.update(TABLE_POWERS, values, FIELD_POWERS_ID+"="+powerId, null);
    }
    
}
